<template>
  <div style="padding: 10px">
    <div>
      <div style="margin: 10px 0">
        <el-button @click="add">新增</el-button>
        <el-button>导入</el-button>
        <el-button>导出</el-button>
      </div>
      <!--    搜索区域-->
      <div style="margin: 10px 0 ">
        <el-input v-model="search" clearable placeholder="请输入内容" style="width: 20%"/>
        <el-button style="margin-left: 5px" type="primary" @click="load">查询</el-button>
      </div>
    </div>
    <!--    功能区域-->

    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column fixed label="Date" prop="date" sortable/>
      <el-table-column label="用户名" prop="username"/>
      <el-table-column label="昵称" prop="nickName"/>
      <el-table-column label="年龄" prop="age"/>
      <el-table-column label="地址" prop="address"/>
      <el-table-column label="性别" prop="sex"/>
      <el-table-column fixed="right" label="Operations">
        <template #default="scope">
          <el-button link type="primary" @click="handleEdit(scope.row)"> 编辑</el-button>
          <el-popconfirm
              :icon="InfoFilled"
              icon-color="#626AEF"
              title="你确定删除吗?"
              width="220"
              @cancel="onCancel"
          >
            <template #reference>
              <el-button type="text">删除</el-button>
            </template>
            <template #actions="{ confirm, cancel }">
              <el-button size="small" @click="cancel">否!</el-button>
              <el-button
                  :disabled="!clicked"
                  size="small"
                  type="danger"
                  @click="confirm"
              >
                是?
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 50%">
      <el-pagination
          v-model:current-page="currentPage4"
          v-model:page-size="pageSize4"
          :background="background"
          :disabled="disabled"
          :page-sizes="pageSize"
          :size="size"
          :total="0"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
      <!--      弹窗-->
      <el-dialog
          v-model="dialogVisible"
          title="Tips"
          width="500"
      >
        <el-form :model="form" label-width="120px">
          <el-form-item label="用户名">
            <el-input v-model="form.username" style="width:80% "/>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickName" style="width:80% "/>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.age" style="width:80% "/>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="form.sex">
              <el-radio label="公">公</el-radio>
              <el-radio label="母">母</el-radio>
              <el-radio label="未知">未知</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="form.address" style="width:80% " type="textarea"/>
          </el-form-item>
        </el-form>
        <span>This is a message</span>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="save">
              确定
            </el-button>
          </div>
        </template>
      </el-dialog>

    </div>
  </div>
</template>

<script>


import request from "@/utils/request";

export default {
  name: 'HomeView',
  components: {},
  data() {
    return {
      form() {
      },
      dialogVisible: false,
      search: '',
      currentPage4: 1,
      pageSize: 10,

      total: 0,
      tableData: [
        {
          date: '2016-05-03',
          username: 'Tom',
          nickName: 'Ci',
          age: '3',
          address: 'No. 189, Grove St, Los Angeles',
          sex: '公',
          tag: 'Home',
        },
        {
          date: '2016-05-02',
          username: 'Tom',
          nickName: 'Ci',
          age: '3',
          address: 'No. 189, Grove St, Los Angeles',
          sex: '公',
          tag: 'Office',
        },
        {
          date: '2016-05-04',
          username: 'Tom',
          nickName: 'Ci',
          age: '3',
          address: 'No. 189, Grove St, Los Angeles',
          sex: '公',
          tag: 'Home',
        },
        {
          date: '2016-05-01',
          username: 'Tom',
          nickName: 'Ci',
          age: '3',
          address: 'No. 189, Grove St, Los Angeles',
          sex: '公',
          tag: 'Office',
        },
      ]
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request.get("/user", {
        params: {
          pageNum: this.currentPage4,
          pageSize: this.pageSize,
          search: this.search
        }

      }).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    add() {
      this.dialogVisible = true
      // 表单清空
      this.form = {}
    },
    // 逻辑
    save: function () {
      // 把数据发送到后台
      if (this.form.id) { //更新
        request.post("http://localhost:9090/user", this.form).then(res => {
          console.log(res)
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })

          }
          this.load()//刷新表格的数据
          this.dialogVisible = false //关闭弹窗
        })

      } else {//新增
        request.put("http://localhost:9090/user", this.form).then(res => {
          console.log(res)
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()//刷新表格的数据
          this.dialogVisible = false //关闭弹窗
        })

      }

    },
    handleEdit(row) {
      /*  form为独立对象*/
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    handleDelete(row) {
      console.log(id)
      request.delete("/http://localhost:9090/user/")

    },


    // 改变当前每页个数触发
    handleSizeChange(pageSize) {
      this.pageSize4 = pageSize
      this.load()
    },
    // 改变当前页码触发
    handleCurrentChange(pageNum) {
      this.currentPage4 = pageNum
      this.load()
    },
    pageSize4() {

    },

  }
}
</script>
